﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using IEx.Common;
using IEx.Model.Partials;
using IEx.Utilities;

namespace IEx.ParserBuilder.Webtretho
{
    public class WebtrethoParser : IHtmlParser
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="item"></param>
        /// <param name="html"></param>
        /// <param name="attachResultItem"></param>
        /// <param name="attachNewItemSource"></param>
        public void Parse(SourceItem item, string html, Action<Model.Partials.Announcement> attachResultItem, Action<SourceItem> attachNewItemSource)
        {
            try
            {
                string startMainContent = "id=\"mainColLeft_reFix\"";
                string endMainContent = "id=\"mainColRight_reFix\"";
                string remainHtml = Utility.GetHtmlBody(html, startMainContent, endMainContent);
                List<Announcement> detectedAnnouncements = new List<Announcement>();
                if (!string.IsNullOrEmpty(remainHtml))
                {
                    Regex regThread = new Regex("<li id=\".*?\" class=\"threadbit_nam_fix_select\">.*?</li>");
                    MatchCollection mThread = regThread.Matches(remainHtml);
                    for (int i = 0; i < mThread.Count; i++)
                    {
                        string threadTitle = string.Empty;
                        string user = string.Empty;
                        string linkThreadDetail = string.Empty;
                        DateTime postDate = DateTime.MinValue;

                        string threadHtml = mThread[i].Value;
                        //Extract title
                        Regex regTitleThread = new Regex("<h3>.*?</h3>");
                        Match mTitleThread = regTitleThread.Match(threadHtml);
                        if (mTitleThread.Success)
                        {
                            string titleThreadHtml = mTitleThread.Value;
                            threadTitle = RegexUtility.ExtractTextFromHtmlTag(titleThreadHtml);
                        }
                        //Extract user post
                        Regex regNameAuPost = new Regex("<li class=\"nameAuPost\">.*?</li>");
                        Match mNameAuPost = regNameAuPost.Match(threadHtml);
                        if (mNameAuPost.Success)
                        {
                            string nameAuPostHtml = mNameAuPost.Value;
                            user = RegexUtility.ExtractTextFromHtmlTag(nameAuPostHtml);
                        }
                        //Extract link detail
                        Regex regLinkDetail = new Regex("<a class=\"title.*?\".*?</a>");
                        Match mLinkDetail = regLinkDetail.Match(threadHtml);
                        if (mLinkDetail.Success)
                        {
                            string linkDetailHtml = mLinkDetail.Value;
                            linkThreadDetail = RegexUtility.ExtractHyperlink(linkDetailHtml);
                        }
                        //Extract thread detail
                        List<Comment> comments = new List<Comment>();
                        if (!string.IsNullOrEmpty(linkThreadDetail))
                        {
                            ExtractCommentsWebtretho(linkThreadDetail, item, ref comments);
                        }

                        if (!string.IsNullOrEmpty(threadTitle) && !string.IsNullOrEmpty(user)
                            && comments.Count > 0)
                        {
                            Announcement ann = new Announcement();
                            ann.SourceId = item.SourceId;
                            ann.Title = threadTitle;
                            ann.SourceCode = item.SourceCode;
                            ann.LangId = item.LangId;
                            ann.InsertedDate = DateTime.Now;
                            ann.DateTime = postDate;
                            ann.DateNumberic = long.Parse(
                                        postDate.ToString("yyyyMMddHHmmss"));
                            ann.SocialNetwork.Comments = comments;
                            ann.SocialNetwork.ScreenName = user;
                            detectedAnnouncements.Add(ann);
                        }
                    }
                    Regex regNextPage = new Regex("<li><a href=\"javascript://\".*?class=\"Selected\">.*?</a></li><li><a.*?>.*?</a></li>");
                    Match mNextPage = regNextPage.Match(remainHtml);
                    if (mNextPage.Success)
                    {
                        string nextPageHtml = mNextPage.Value;
                        string link = RegexUtility.ExtractHyperlink(nextPageHtml);
                        if (!string.IsNullOrEmpty(link))
                        {
                            item.Url = link;
                        }
                        else
                        {
                            item.Url = string.Empty;
                        }
                    }
                }

                // check if has next page 
                // next page counts as a new request, so it needed insert into request queue

                if (CheckNextPageWebtretho(item.Url))
                {
                    SourceItem nItem = new SourceItem(item.Url, item.LangId, item.SourceCode, item.SearchCode, item.SourceId, item.DateTimeFormat);
                    if (!string.IsNullOrEmpty(item.RequestBuilderType))
                        nItem.RequestBuilderType = item.RequestBuilderType;

                    if (!string.IsNullOrEmpty(item.HtmlParserType))
                        nItem.HtmlParserType = item.HtmlParserType;

                    nItem.PrevResponseHtml = html;
                    attachNewItemSource(nItem);
                }

                // insert into download queue when no error in parsing
                foreach (Announcement ann in detectedAnnouncements)
                {
                    attachResultItem(ann);
                }

                // if this request is fail request in previous scanning, 
                // remove it when success in current scanning
                if (item.FailId > 0)
                    Utility.DeleteFailRequests(item.FailId);
            }
            catch (Exception ex)
            {
                // if has an error, save fail request to next time scanning
                if (item.FailId == 0)
                {
                    item.FailId = DateTime.Now.Ticks;
                    Utility.SaveFailRequests(item);
                }

                // then write log
                string message = string.Format(@"
            Company code:   {0}
            HTML Parsing:   Fail
            Request URL:    {1}
            Search key:     {2}", item.SourceCode, item.Url, item.SearchCode);
                Logger.WriteError(message, ex);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="url"></param>
        /// <param name="item"></param>
        /// <param name="comments"></param>
        private static void ExtractCommentsWebtretho(string url, SourceItem item, ref List<Comment> comments)
        {
            Logger.Debug("***SCANNING POST COMMENTS URL: " + url);
            HttpStatusCode status = HttpStatusCode.Accepted;
            string html = Utility.LoadHtml(url, ref status);
            Uri newUri = Utility.CreateNewUriForNextPage(url);
            string nextPageUrl = string.Empty;
            try
            {
                if (!string.IsNullOrEmpty(html))
                {
                    string startHtml = "id=\"mainColLeft_reFix\"";
                    string endHtml = "id=\"mainColRight_reFix\"";
                    string remainHtml = Utility.GetHtmlBody(html, startHtml, endHtml);

                    if (!string.IsNullOrEmpty(remainHtml))
                    {
                        Regex regCommentBlock = new Regex("class=\"postbit postbitim postcontainer\" id=\".*?\">.*?<div class=\"postfoot\">");
                        MatchCollection mCommentBlock = regCommentBlock.Matches(remainHtml);
                        for (int i = 0; i < mCommentBlock.Count; i++)
                        {
                            string commentContent = string.Empty;
                            string user = string.Empty;
                            string dateTime = string.Empty;
                            string time = string.Empty;
                            string date = string.Empty;
                            DateTime postDate = DateTime.Now;

                            string commentBlockHtml = mCommentBlock[i].Value;
                            //Extract Comment content
                            Regex regContentComment = new Regex("<div class=\"content\">.*?</div>");
                            Match mContentComment = regContentComment.Match(commentBlockHtml);
                            if (mContentComment.Success)
                            {
                                string commentContentHtml = mContentComment.Value;
                                commentContent = RegexUtility.ExtractTextFromHtmlTag(commentContentHtml).Trim();
                            }

                            //Extract User info
                            Regex regUserInfo = new Regex("<div class=\"userinfo\">.*?</div>");
                            Match mUser = regUserInfo.Match(commentBlockHtml);
                            if (mUser.Success)
                            {
                                string userHtml = mUser.Value;
                                user = RegexUtility.ExtractTextFromHtmlTag(userHtml).Trim();
                            }

                            //Extract Date time
                            Regex regTime = new Regex("<span class=\"time\">.*?</span>");
                            Match mTime = regTime.Match(commentBlockHtml);
                            if (mTime.Success)
                            {
                                string timeHtml = mTime.Value;
                                time = RegexUtility.ExtractTextFromHtmlTag(timeHtml).Trim();

                            }

                            Regex regDate = new Regex("<span class=\"date\">.*?</span>.*?</span>");
                            Match mDate = regDate.Match(commentBlockHtml);
                            if (mDate.Success)
                            {
                                string dateHtml = mDate.Value;
                                date = RegexUtility.ExtractTextFromHtmlTag(dateHtml).Trim();
                                date = date.Replace(time, string.Empty).Trim();
                            }

                            if (!string.IsNullOrEmpty(date) && !string.IsNullOrEmpty(time))
                            {
                                dateTime = date + " " + time;
                                postDate = Utility.ConvertToDateTimeObject(dateTime, item.DateTimeFormat);
                            }

                            if (!string.IsNullOrEmpty(commentContent) && !string.IsNullOrEmpty(user))
                            {
                                comments.Add(new Comment()
                                {
                                    CommentContent = commentContent,
                                    DateTime = postDate,
                                    UserName = user,
                                    DateNumberic = long.Parse(
                                        postDate.ToString("yyyyMMddHHmmss")),
                                });
                            }
                        }
                        Regex regNextPage = new Regex("<li><a href=\"javascript://\".*?class=\"Selected\">.*?</a></li><li><a.*?>.*?</a></li>");
                        Match mNextPage = regNextPage.Match(remainHtml);
                        if (mNextPage.Success)
                        {
                            string nextPageHtml = mNextPage.Value;
                            string link = RegexUtility.ExtractHyperlink(nextPageHtml);
                            if (!string.IsNullOrEmpty(link))
                            {
                                nextPageUrl = link;
                            }
                            else
                            {
                                nextPageUrl = string.Empty;
                            }
                        }
                    }
                }

                // if this request is fail request in previous scanning, 
                // remove it when success in current scanning
                if (item.FailId > 0)
                    Utility.DeleteFailRequests(item.FailId);

                Logger.WriteLogInfo("***DONE SCANNING POST COMMENT URL: " + url);
            }
            catch (Exception ex)
            {
                // if has an error, save fail request to next time scanning
                if (item.FailId == 0)
                {
                    item.FailId = DateTime.Now.Ticks;
                    Utility.SaveFailRequests(item);
                }

                // then write log
                string message = string.Format(@"
            Extract post-comment details
            Source code:   {0}
            HTML Parsing:   Fail
            Request URL:    {1}
            Page number:     {2}", item.SourceCode, url, nextPageUrl);
                Logger.WriteError(message, ex);
            }
            // check if has next page 
            // next page counts as a new request, so it needed insert into request queue
            if (CheckNextPageWebtretho(nextPageUrl))
            {
                ExtractCommentsWebtretho(nextPageUrl, item, ref comments);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        private static bool CheckNextPageWebtretho(string response)
        {
            bool hasNextPage = !string.IsNullOrEmpty(response) && string.Compare(response, "javascript://") != 0;
            return hasNextPage;
        }

    }
}
